<template>
  <view class="commonLim-list">
    <view class="searchclass">
      <view class="left">
        <up-search
          v-model="query.trackingNo"
          placeholder="请输入随工单编号"
          :clearabled="true"
          :showAction="false"
          @search="handleSearch"
        />
        <view v-show="isshow1" class="left">
          <div @click="handlefocus1">
            <up-search
              v-model="query.materialTypeName"
              placeholder="请选择材质"
              :clearabled="true"
              :showAction="false"
              disabled
            />
          </div>
          <div @click="handlefocus2">
            <up-search
              v-model="query.specificationName"
              placeholder="请选择规格型号"
              :clearabled="true"
              :showAction="false"
              disabled
            />
          </div>
        </view>
      </view>
      <view class="right">
        <up-button
          type="primary"
          @click="handleSearch"
          size="small"
          shape="circle"
          class="first"
          >搜索</up-button
        >
        <up-button
          type="primary"
          @click="handlecancel"
          size="small"
          shape="circle"
          >重置</up-button
        >
      </view>
      <image
        src="../../static/tabs/qr.png"
        @click="handlecan1"
        class="mgl10px"
        style="width: 80rpx; height: 50rpx"
      />
    </view>
    <view class="upCcon">
      <up-icon
        :name="iconName"
        size="50rpx"
        color="#3c9cff"
        @click="handleicon"
      ></up-icon>
    </view>
    <scroll-view
      class="commonLim-listView"
      scroll-y
      @scrolltolower="onScrollToBottom"
    >
      <view v-show="state.rows.length">
        <view
          v-for="(item, index) in state.rows"
          :key="item.id"
          class="commonLim-listView-item"
        >
          <view class="commonLim-listView-item-title">
            <label>{{ item.batchNo }}</label>
            <text class="mgr40">工序：{{ item.processName }}</text>
            <text>序号：{{ index + 1 }}</text>
          </view>
          <view class="commonLim-listView-item-content">
            <view class="half width100">
              <label>随工单编号:</label>
              <text>{{ item.trackingNo }}</text>
            </view>
            <view class="half width100">
              <label>物料编码:</label>
              <text>{{ item.materialCode }}</text>
            </view>
            <view class="half width100">
              <label>材质:</label>
              <text>{{ item.materialTypeName }}</text>
            </view>
            <view class="half width100">
              <label>规格型号:</label>
              <text>{{ item.specificationName }}</text>
            </view>
            <view class="half width100">
              <label>炉号:</label>
              <text>{{ item.heatNo }}</text>
            </view>
            <view class="half width100">
              <label>机台编号:</label>
              <text>{{ item.machineNumber }}</text>
            </view>
            <view class="half width100">
              <label>分轮编号:</label>
              <text>{{ item.roundNo }}</text>
            </view>
            <view class="half width100">
              <label>分轮重量:</label>
              <text>{{ item.roundWeight }}</text>
            </view>
            <view class="half width100">
              <label>客户编码:</label>
              <text>{{ item.customerAbbr }}</text>
            </view>
            <view class="half width100">
              <label>分卷操作人:</label>
              <text>{{ item.operatorName }}</text>
            </view>
            <view class="half width100">
              <label>分卷操作日期:</label>
              <text>{{ item.operationDate }}</text>
            </view>
            <view class="half width100" v-if="title != '待检确认'">
              <label>称重:</label>
              <text>{{ item.weight }}</text>
            </view>
          </view>
          <view class="commonLim-listView-item-button">
            <view class="box">
              <up-button
                type="error"
                text="打印标签"
                size="small"
                @click="handleprint(item)"
                v-if="['待检确认', '称重'].includes(title)"
              ></up-button>
              <up-button
                type="error"
                text="批次拆分"
                size="small"
                @click="handlePicicaifen(item)"
                v-if="title == '待检确认'"
              ></up-button>
              <up-button
                type="primary"
                text="称重"
                size="small"
                @click="handleweighing(item)"
                v-if="title == '称重'"
              ></up-button>
              <up-button
                type="primary"
                text="确认"
                size="small"
                @click="handleSure(item)"
                v-if="queren(item)"
              ></up-button>
              <up-button
                type="primary"
                text="称重修改"
                size="small"
                @click="handleEditeweighing(item)"
                v-if="title == '已检查询'"
              ></up-button>
            </view>
          </view>
        </view>
      </view>
      <up-empty mode="data" v-show="state.rows.length == 0"> </up-empty>
      <up-loadmore
        v-if="state.rows.length"
        :status="loadStatus"
        :icon="true"
        loadmore-text="轻轻上拉获取更多"
        loading-text="努力加载中..."
        nomore-text="没有更多了"
        @loadmore="handleloadMore"
      />
    </scroll-view>
    <view class="pageClass">
      <up-popup :show="showReject1" mode="bottom" closeOnClickOverlay>
        <view class="btn-group">
          <text class="cancel" @click="showReject1 = false">取消</text>
          <text class="confirm" @click="handleConfirm1">确定</text>
        </view>
        <view class="popup-content">
          <view class="fenlun">
            <view class="titleClass">批次拆分数：</view>
            <up-input
              placeholder="请输入批次拆分数（必填）"
              v-model="currentTable1.splitCount"
              type="number"
            ></up-input>
          </view>
        </view>
      </up-popup>
    </view>
    <view class="pageClass">
      <up-popup
        :show="showReject2"
        mode="bottom"
        closeOnClickOverlay
        :teleport="false"
      >
        <view class="btn-group">
          <text class="cancel" @click="showReject2 = false">取消</text>
          <text class="confirm" @click="handleConfirm2">确定</text>
        </view>
        <view class="popup-content">
          <view>
            <view class="upflex10 algn">
              <view> 是否异常：</view>
              <view>
                <radio-group @change="onGenderChange">
                  <label class="mgr40" v-for="item in radiolist" :key="item.id">
                    <radio
                      :value="item.id"
                      :checked="currentTable2.abnormal == item.id"
                    />
                    {{ item.name }}
                  </label>
                </radio-group>
              </view>
            </view>
            <view class="upflex10 algn" v-show="currentTable2.abnormal == 1">
              <view> 原因：</view>
              <up-input
                placeholder="请输入问题"
                v-model.trim="currentTable2.reason"
              ></up-input>
            </view>
          </view>
        </view>
      </up-popup>
    </view>
    <view class="pageClass">
      <up-popup :show="showReject3" mode="bottom" closeOnClickOverlay>
        <view class="btn-group">
          <text class="cancel" @click="showReject3 = false">取消</text>
          <text class="confirm" @click="handleConfirm3">确定</text>
        </view>
        <view class="popup-content">
          <view class="fenlun">
            <view class="titleClass">{{ currentTable3.addOrEdit }}重量：</view>
            <up-input
              placeholder="请输入重量（必填）"
              v-model="currentTable3.weight"
              type="number"
            ></up-input>
          </view>
        </view>
      </up-popup>
    </view>
    <PrintLabel
      @closeFn="showTemReject = false"
      :showTemReject="showTemReject"
      :currentObject="currentObject"
    />
  </view>
</template>

<script setup>
import {
  managementPage,
  detailPageWeighing,
  yiDetailPage,
  detailSplitBatch,
  detailBulkConfirm,
  confirmException,
  pendingInbound,
  confirmWeighing,
  editeWeighing,
  saomarequest1,
} from "./api.js";
import { commonListsFn } from "@/utils/index.js";
import { useMemberStore } from "@/stores/modules/member";
import { onCan } from "@/utils/index.js";
const memberStore = useMemberStore();
const deprops = defineProps(["title"]);
const iconName = ref("arrow-down");
const currentTable1 = ref({});
const currentTable2 = ref({});
const currentTable3 = ref({});
const showReject1 = ref(false);
const showReject2 = ref(false);
const showReject3 = ref(false);
const showTemReject = ref(false);
const currentObject = ref(null);
const queren = computed(() => {
  return (item) => {
    if (deprops.title == "待检确认") {
      return true;
    } else {
      if (deprops.title == "称重") {
        if (item.weight > 0) {
          return true;
        } else {
          return false;
        }
      } else {
        return false;
      }
    }
  };
});
const radiolist = ref([
  {
    id: 0,
    name: "否",
  },
  {
    id: 1,
    name: "是",
  },
]);
const isshow1 = computed(() => {
  if (iconName.value == "arrow-down") {
    return false;
  } else {
    return true;
  }
});
const handleicon = () => {
  if (iconName.value == "arrow-down") {
    iconName.value = "arrow-up";
  } else {
    iconName.value = "arrow-down";
  }
};
const callback1 = async (obj) => {
  if (deprops.title == "待检确认") {
    return managementPage(obj);
  } else if (deprops.title == "称重") {
    return detailPageWeighing(obj);
  } else if (deprops.title == "已检查询") {
    return yiDetailPage(obj);
  }
};
const {
  query,
  state,
  loadStatus,
  handleSearch,
  handlecancel,
  onScrollToBottom,
} = commonListsFn(callback1);
const handlePicicaifen = (item) => {
  showReject1.value = true;
  currentTable1.value.detailId = item.id;
  currentTable1.value.splitCount = "";
};

const handleConfirm1 = async () => {
  if (!+currentTable1.value.splitCount) {
    return uni.showToast({
      icon: "none",
      title: "请输入批次拆分数",
    });
  }
  try {
    uni.showLoading({
      title: "加载中",
    });
    await detailSplitBatch(
      currentTable1.value.detailId,
      +currentTable1.value.splitCount
    );
    uni.showToast({ icon: "success", title: "批次拆分成功" });
    handleSearch();
  } finally {
    showReject1.value = false;
    uni.hideLoading();
  }
};
const handleSure = async (item) => {
  if (deprops.title == "待检确认") {
    showReject2.value = true;
    currentTable2.value.id = item.id;
    currentTable2.value.abnormal = 0;
    currentTable2.value.reason = "";
  } else if (deprops.title == "称重") {
    await pendingInbound({
      ids: [item.id],
    });
    uni.showToast({ icon: "success", title: "确认成功" });
    handleSearch();
  }
};
const onGenderChange = (e) => {
  currentTable2.value.abnormal = e.detail.value;
};
const handleConfirm2 = async () => {
  try {
    if (![0, 1].includes(currentTable2.value.abnormal)) {
      return uni.showToast({
        icon: "none",
        title: "请选择是否异常",
      });
    }
    if (currentTable2.value.abnormal == 1) {
      if (!currentTable2.value.reason) {
        return uni.showToast({
          icon: "none",
          title: "请输入原因",
        });
      }
      uni.showLoading({
        title: "加载中",
      });
      await confirmException({
        ids: [currentTable2.value.id],
        reason: currentTable2.value.reason,
      });
    } else {
      uni.showLoading({
        title: "加载中",
      });
      await detailBulkConfirm({
        ids: [currentTable2.value.id],
      });
    }
    showReject2.value = false;
    uni.showToast({ icon: "success", title: "确认成功" });
    handleSearch();
  } catch (err) {
    showReject2.value = false;
  } finally {
    uni.hideLoading();
  }
};

const handleweighing = (item) => {
  showReject3.value = true;
  currentTable3.value.id = item.id;
  currentTable3.value.weight = "";
  currentTable3.value.addOrEdit = "填写";
};
const handleEditeweighing = (item) => {
  showReject3.value = true;
  currentTable3.value.id = item.id;
  currentTable3.value.weight = item.weight;
  currentTable3.value.addOrEdit = "修改";
};
const handleConfirm3 = async () => {
  if (!+currentTable3.value.weight) {
    return uni.showToast({
      icon: "none",
      title: "重量不能为空或0",
    });
  }
  try {
    uni.showLoading({
      title: "加载中",
    });
    if (currentTable3.value.addOrEdit == "填写") {
      await confirmWeighing(currentTable3.value.id, currentTable3.value.weight);
    } else {
      await editeWeighing(currentTable3.value.id, {
        weight: currentTable3.value.weight,
      });
    }
    uni.showToast({
      icon: "success",
      title: currentTable3.value.addOrEdit == "填写" ? "称重成功" : "修改成功",
    });
    handleSearch();
  } finally {
    showReject3.value = false;
    uni.hideLoading();
  }
};
const handlefocus1 = () => {
  uni.navigateTo({
    url: `/pages/searchcaizhi/searchcaizhi?from=caizhi`,
  });
};
const handlefocus2 = async () => {
  if (query.value.materialTypeId) {
    uni.navigateTo({
      url: `/pages/searchcaizhi/searchcaizhi?from=guigexinghao&pId=${query.value.materialTypeId}`,
    });
  } else {
    uni.showToast({
      icon: "none",
      title: "请选择材质",
    });
  }
};
const fengzhuang1 = async (value) => {
  const res = await saomarequest1(value);
  if (res) {
    state.rows.push(res);
  }
};
const handlecan1 = () => {
  onCan(fengzhuang1);
};
const handleprint = (item) => {
  showTemReject.value = true;
  currentObject.value = item;
};
onLoad(() => {
  uni.setNavigationBarTitle({
    title: deprops.title,
  });
});
onShow(() => {
  if (memberStore.from_Page == "caizhi") {
    query.value.materialTypeId = memberStore.zemId;
    query.value.materialTypeName = memberStore.zemName;
    query.value.specificationId = "";
    query.value.specificationName = "";
  } else if (memberStore.from_Page == "guigexinghao") {
    query.value.specificationId = memberStore.zemId;
    query.value.specificationName = memberStore.zemName;
  }
  memberStore.zemId = "";
  memberStore.zemName = "";
  memberStore.from_Page = "";
  handleSearch();
});
</script>

<style lang="scss" scoped>
</style>